package jsyf.oa.service.wechartSec;

import com.alibaba.dubbo.common.utils.StringUtils;
import com.alibaba.fastjson.JSON;

import com.alibaba.fastjson.JSONObject;
import jsyf.com.util.FastJsonConvert;
import jsyf.oa.entity.wechartSec.WechartDeviceEntity;
import jsyf.oa.facade.wechartSec.WechartDeviceFacade;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import jsyf.oa.dao.wechartSec.WechartDeviceDao;

import java.util.List;

/**
 * Created by Administrator on 2018/4/8.
 */
@Service
@Transactional(rollbackFor = Exception.class)
@com.alibaba.dubbo.config.annotation.Service(protocol = {"dubbo"})
public class WechartDeviceService implements WechartDeviceFacade {

    @Autowired
    private WechartDeviceDao device2Dao;

    @Override
    public String save(String jsonData) {
        WechartDeviceEntity entity = JSON.toJavaObject(JSON.parseObject(jsonData), WechartDeviceEntity.class);
        Integer rows;
        if ("0".equals(entity.getId()) || StringUtils.isBlank(entity.getId())) {
            rows = device2Dao.add(entity);
        } else {
            WechartDeviceEntity deviceEntity = (WechartDeviceEntity) device2Dao.getIBaseEntityById(entity.getId());
            deviceEntity.setCreateUserId(entity.getCreateUserId());
            deviceEntity.setCreateTime(entity.getCreateTime());
            deviceEntity.setDeviceNo(entity.getDeviceNo());
            deviceEntity.setProvince(entity.getProvince());
            deviceEntity.setCity(entity.getCity());
            deviceEntity.setCounty(entity.getCounty());
            deviceEntity.setBrandId(entity.getBrandId());
            deviceEntity.setHours(entity.getHours());
            deviceEntity.setPrice(entity.getPrice());
            deviceEntity.setProcedureFlag(entity.getProcedureFlag());
            deviceEntity.setCoverImgUrl(entity.getCoverImgUrl());
            deviceEntity.setDelFlag(entity.getDelFlag());
            rows = device2Dao.update(deviceEntity);
        }
        return rows == 0 ? "" : entity.getId();
    }

    @Override
    public JSONObject getDeviceListByPage(String jsonData) {
        StringBuffer sql = new StringBuffer();
        JSONObject jsonParams = FastJsonConvert.convertJSONToObject(jsonData, JSONObject.class);
        String createUserId = jsonParams.getString("createUserId");
        if (!org.apache.commons.lang3.StringUtils.isBlank(createUserId)) {
            sql.append(" AND T_SEC_DEVICE.CREATE_USER_ID = " + "'" + createUserId + "'");

        }
        String requestPage;
        String pageSize;
        if (jsonParams.containsKey("requestPage") && jsonParams.containsKey("pageSize")) {
            requestPage = jsonParams.getString("requestPage");
            pageSize = jsonParams.getString("pageSize");
        } else {
            requestPage = "1";
            pageSize = Integer.MAX_VALUE + "";
        }
        if (org.apache.commons.lang3.StringUtils.isBlank(requestPage) && org.apache.commons.lang3.StringUtils.isBlank(pageSize)) {
            requestPage = "1";
            pageSize = Integer.MAX_VALUE + "";
        }
        List<JSONObject> records = device2Dao.getJSONListByPage(sql.toString(), new Object[]{}, Integer.valueOf(requestPage), Integer.valueOf(pageSize));
        int recordCount = device2Dao.getPageCount(sql.toString(), new Object[]{});
        int recordSize = Integer.valueOf(pageSize);
        //如果整除表示正好分N页，如果不能整除在N页的基础上+1页
        int pageCount = recordCount % recordSize == 0 ? recordCount / recordSize : (recordCount / recordSize) + 1;
        JSONObject result = new JSONObject();
        JSONObject objSendData = new JSONObject();
        //子对象
        objSendData.put("recordCount", recordCount);
        objSendData.put("pageCount", pageCount);
        objSendData.put("requestPage", 1);
        result.put("pager", objSendData);
        // 子数组对象
        result.put("records", records);
        return result;

    }
    @Override
    public JSONObject getDeviceById(String id) {
        JSONObject device = device2Dao.getJSONListByCondition(" AND id=?",new Object[]{id}).get(0);
        return device;
    }
}